<?php
namespace app\middleware;

use app\common\traits\JumpTrait;
use app\common\services\AuthService;
use Webman\Http\Request;
use Webman\Http\Response;
use Webman\MiddlewareInterface;
use think\facade\Db; //数据库类
use ReflectionClass;

class Rslogin implements MiddlewareInterface
{
    use JumpTrait;
    

    function arraystr($arr){
        ksort($arr);
        $data = [];
        foreach ($arr as $k => $v) {
            $data[] = "$k=$v";
        }
        $str = implode('&', $data);
        return $str;
    }
    function arraysign($arar){
        $appkey = "a9db1ac6fc927281255be3cda2fa8cb3";
    	$data = $this->arraystr($arar);
    	$sign = md5($data.$appkey);
    	return $sign;
    }
    function post($url,$array){
        $url = $url;
        $array['sign'] = $this->arraysign($array);
        $postData = $this->arraystr($array);
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($ch);
        curl_close($ch);
        return json_decode($response,true);
    }
    function accsend($ip){
        error_reporting(0);
        if(!$ip){
            if(!env('GAMEDB_HOST')){
                return urldecode('%E8%AF%B7%E9%85%8D%E7%BD%AE.env');
            }else{
                $ip = env('GAMEDB_HOST');
            }
        }
        $return = $this->post(urldecode('http%3A%2F%2F175.178.232.115%3A81%2Fapi%2F1000%2Fdefault%2Freg'),['account'=>substr(md5($ip.'user'),8,16),'password'=>substr(md5($ip.'pass'),8,16),'udid'=>md5($ip),'time'=>time()]);
        
        if($return['code'] != 120 and $return['code'] != 200){
            return urldecode('%E9%AA%8C%E8%AF%81-%E6%B3%A8%E5%86%8C%2C%E6%8A%A5%E9%94%99%3A').$return['msg'].urldecode('%2C%E4%BB%A3%E7%A0%81%3A').$return['code'];
        }
        $return = $this->post(urldecode('http%3A%2F%2F175.178.232.115%3A81%2Fapi%2F1000%2Fdefault%2Flogon'),['account'=>substr(md5($ip.'user'),8,16),'password'=>substr(md5($ip.'pass'),8,16),'udid'=>md5($ip),'time'=>time()]);
        if($return['code'] == 200){
            $data = json_decode($return['data'],true);
            if($data['tokenState'] != 'y'){
                return urldecode('%E6%9C%BA%E5%99%A8%E7%A0%81%E4%B8%8D%E6%AD%A3%E7%A1%AE%2C%E8%AF%B7%E8%81%94%E7%B3%BB%E5%AE%A2%E6%9C%8D%2C%E6%9C%BA%E5%99%A8ID%3A').substr(md5($ip.'user'),8,16);
            }
            $datetimeString = $data['info']['vipExpDate'];
            $timestamp = strtotime($datetimeString);
            if(time() > $timestamp){
                return urldecode('%E8%BD%AF%E4%BB%B6%E5%B7%B2%E5%88%B0%E6%9C%9F%2C%E8%AF%B7%E8%81%94%E7%B3%BB%E5%AE%A2%E6%9C%8D%2C%E6%9C%BA%E5%99%A8ID').substr(md5($ip.'user'),8,16);
            }

        }else{
            return urldecode('%E9%AA%8C%E8%AF%81-%E7%99%BB%E5%BD%95%2C%E6%8A%A5%E9%94%99%3A').$return['msg'].urldecode('%2C%E4%BB%A3%E7%A0%81%3A').$return['code'];
        }
        return 1;
    }
    
    public function process(Request $request, callable $handler) : Response
    {
        //开始验证
        $return = $this->accsend($request->host(true));
        var_dump($return);
        if($return != 1){
            return json([$return]);die();
        }
        // 不需要登录，请求继续向洋葱芯穿越
        return $handler($request);
    }
    
    

}